/*
 * @Author: rogi
 * @Date: 2022-07-19 08:56:20
 * @LastEditTime: 2022-12-26 14:06:41
 * @LastEditors: rogi
 * @Description: 描述
 */
// import router from '@/router/index.js'
import { useRoute } from 'vue-router'
import routerObj from '@/router/index.js';
import Cookie from 'js-cookie';
import { write,read,remove} from "@/utils/storage";

export default {
  /**
   * @description: 存储菜单
   * @param {*} store
   * @param {*} data
   * @return {*}
   */  
  SET_MENU(store, data) {
    store.menu = data;
    localStorage.setItem('menu', JSON.stringify(data));
    this.commit('common/ADD_MENU')
  },
  SET_AREA(store, data) {
    store.areaData = data;
    localStorage.setItem('area', JSON.stringify(data));
  },
  /**
   * @description: 添加路由
   */  
  ADD_MENU(store, APProuter) {
    let router = routerObj;
    // 获取view下所有的.vue文件集合
    const modules = import.meta.glob("../../../views/**/**/**/**/**.vue");
    // 从vue获取菜单列表
    const menuArray = store.menu || [];
    function ergodic(list, parent = {}) {
      list.forEach(el => {
        // if () {}
        const parentBreadcrumb = parent.meta && parent.meta.Breadcrumb.length ? parent.meta.Breadcrumb : []
        el.meta.Breadcrumb = [...parentBreadcrumb, el.meta.title]
        // 判断菜单是否为外部页面
        if (el.meta.componentName === 'IframePageView') {
          el.path = `${parent.path}/${el.path}`
        } else {
          // 从vue文件集合与菜单项做匹配取值
          el.component = modules[`../../../views/${el.component}.vue`]
        }
        // 添加菜单
        router && router.addRoute('layout', el)
        // 如果有子级菜单自调用,新添加菜单
        if (el.children && el.children.length >= 1) {
          ergodic(el.children, el)
        }
      })
    };
    ergodic(menuArray);
  },

  /**
   * @description: 添加历史路由
   * @param {Object} route 路由
   */
  ADD_HISTORY_ROUTE(store, route) {
    const historyRoutes = store.historyRoutes;
    // 判断是否存在改路由
    console.log('321321312', route)
    const index = historyRoutes.findIndex(el => el.path === route.path)
    if (!route || route.path === '/login' || route.name === '404') {
      return false
    } else if (index !== -1) {
      store.historyRoutes.splice(index, 1, route)
      return false
    }
    store.historyRoutes.push(route)
  },
  /**
   * @description: 删除历史路由,历史路由会联动历史路由的标签,被删除的默认进入到他前一个历史路由
   * @param {String} path 需要被删除路由的path (path & index)二选一
   * @param {Number} index 需要被删除路由的下标 (path & index)二选一
   * @return {*}
   */  
  REMOVE_ROUTE(store, { path = '', index = 0 }) {
    let ind = index;
    const historyRoutes = store.historyRoutes;
    if (path) {
      ind = historyRoutes.findIndex(el => el.path === path);
    }
    historyRoutes.splice(ind, 1);
    routerObj.push(historyRoutes[ind - 1])
  },
  EMPTY_HISTORY_ROUTE(store) {
    store.historyRoutes = [];
  },
  USER_LOGIN_OUT() {
    remove('X-Access-Token')
    localStorage.removeItem('menu')
    this.commit('common/EMPTY_HISTORY_ROUTE')
    const route = useRoute();
    if (location.pathname !== '/login') {
      routerObj.push(`/login?url=${location.pathname}`)
    }
  },
  /**
   * @description: 去除头部的标题方法，具体调用store.commit('common/ADD_HIDE_TITLE_ROUTES'),不需要额外入参
   * @param {*} store
   * @return {*}
   */  
  ADD_HIDE_TITLE_ROUTES(store) {
    const route = useRoute();
    store.hideTitleRoutes[route.path] = route
  },
  SET_BURROM_All(store, data) {
    sessionStorage.setItem('buttonList', JSON.stringify(data))
    store.buttonList = data
  },
  SET_MENU(store, data) {
    sessionStorage.setItem('menuList', JSON.stringify(data))
    store.menuList = data
  },
  SET_ORDERDETAIL(store, data) {
    sessionStorage.setItem('orderDetail', JSON.stringify(data))
    store.orderDetail = data
  }
}